<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房间id" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房间id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区名称" prop="communityId">
        <el-input
          v-model="queryParams.communityName"
          placeholder="请输入小区id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼栋名称" prop="buildingId">
        <el-input
          v-model="queryParams.buildingName"
          placeholder="请输入楼栋id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单元名称" prop="unitId">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单元id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="roomLevel">
        <el-input
          v-model="queryParams.roomLevel"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间编号" prop="roomCode">
        <el-input
          v-model="queryParams.roomCode"
          placeholder="请输入房间编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋建筑面积" prop="roomAcreage">
        <el-input
          v-model="queryParams.roomAcreage"
          placeholder="请输入房屋建筑面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算费系数" prop="roomCost">
        <el-input
          v-model="queryParams.roomCost"
          placeholder="请输入算费系数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否商铺" prop="roomIsShop">
        <el-input
          v-model="queryParams.roomIsShop"
          placeholder="请输入是否商铺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否商品房" prop="roomSCommercialHouse">
        <el-input
          v-model="queryParams.roomSCommercialHouse"
          placeholder="请输入是否商品房"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:room:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:room:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:room:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:room:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="roomId" type="index" />
      <el-table-column label="小区名称" align="center" prop="community.communityName" />
      <el-table-column label="楼栋名称" align="center" prop="building.buildingName" />
      <el-table-column label="单元名称" align="center" prop="unit.unitName" />
      <el-table-column label="楼层" align="center" prop="roomLevel" />
      <el-table-column label="房间编号" align="center" prop="roomCode" />
      <el-table-column label="房间名称" align="center" prop="roomName" />
      <el-table-column label="房屋建筑面积" align="center" prop="roomAcreage" />
      <el-table-column label="算费系数" align="center" prop="roomCost" />
      <el-table-column label="房屋状态" align="center" prop="roomStatus" />
      <el-table-column label="是否商铺" align="center" prop="roomIsShop" />
      <el-table-column label="是否商品房" align="center" prop="roomSCommercialHouse" />
      <el-table-column label="房屋户型" align="center" prop="roomHouseType" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:room:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:room:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="房间id" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间id" />
        </el-form-item>-->

        <el-form-item label="小区名称" prop="communityId">
<!--          <el-input v-model="form.communityId" placeholder="请输入小区id" />-->
          <el-select v-model="form.communityId" clearable placeholder="请选择">
            <el-option
              v-for="item in communityList"
              :key="item.communityId"
              :label="item.communityName"
              :value="item.communityId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="楼栋名称" prop="buildingId">
<!--          <el-input v-model="form.buildingId" placeholder="请输入楼栋id" />-->
          <el-select v-model="form.buildingId" clearable placeholder="请选择">
            <el-option
              v-for="item in buildingList"
              :key="item.buildingId"
              :label="item.buildingName"
              :value="item.buildingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元名称" prop="unitId">
<!--          <el-input v-model="form.unitId" placeholder="请输入单元id" />-->
          <el-select v-model="form.unitId" clearable placeholder="请选择">
            <el-option
              v-for="item in unitList"
              :key="item.unitId"
              :label="item.unitName"
              :value="item.unitId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="楼层" prop="roomLevel">
          <el-input v-model="form.roomLevel" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="房间编号" prop="roomCode">
          <el-input v-model="form.roomCode" placeholder="请输入房间编号" />
        </el-form-item>
        <el-form-item label="房间名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房间名称" />
        </el-form-item>
        <el-form-item label="房屋建筑面积" prop="roomAcreage">
          <el-input v-model="form.roomAcreage" placeholder="请输入房屋建筑面积" />
        </el-form-item>
        <el-form-item label="算费系数" prop="roomCost">
          <el-input v-model="form.roomCost" placeholder="请输入算费系数" />
        </el-form-item>
        <el-form-item label="是否商铺" prop="roomIsShop">
<!--          <el-input v-model="form.roomIsShop" placeholder="请输入是否商铺" />-->
          <el-radio v-model="form.roomIsShop" label="Y">是</el-radio>
          <el-radio v-model="form.roomIsShop" label="N">否</el-radio>

        </el-form-item>
        <el-form-item label="是否商品房" prop="roomSCommercialHouse">
<!--          <el-input v-model="form.roomSCommercialHouse" placeholder="请输入是否商品房" />-->
          <el-radio v-model="form.roomSCommercialHouse" label="Y">是</el-radio>
          <el-radio v-model="form.roomSCommercialHouse" label="N">否</el-radio>

        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom } from "@/api/zy/room";
import {listCommunity} from "@/api/zy/community";
import {listBuilding} from "@/api/zy/building";
import {listUnit} from "@/api/zy/unit";

export default {
  name: "Room",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间 表格数据
      roomList: [],
      // 小区 表格数据
      communityList:[],
      // 楼栋 表格数据
      buildingList:[],
      // 单元 表格数据
      unitList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomId: null,
        communityId: null,
        buildingId: null,
        unitId: null,
        roomLevel: null,
        roomCode: null,
        roomName: null,
        roomAcreage: null,
        roomCost: null,
        roomStatus: null,
        roomIsShop: null,
        roomSCommercialHouse: null,
        roomHouseType: null,
        communityName:null,
        buildingName:null,
        unitName:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListCommunityName()
    this.getListBuildingName()
    this.getListUnitName()
  },
  methods: {
    /** 小区的名称 */
    getListCommunityName(){
      this.loading=true;
      listCommunity().then(resp=>{
        console.log(resp)
        this.communityList = resp.rows;
        this.total = resp.total;
        this.loading = false;
      });
    },
    /** 查询楼栋 列表 */
    getListBuildingName() {
      this.loading = true;
      listBuilding(this.queryParams).then(response => {
        console.log(response)
        this.buildingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询单元 列表 */
    getListUnitName() {
      this.loading = true;
      listUnit(this.queryParams).then(response => {
        this.unitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询房间 列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        communityId: null,
        buildingId: null,
        unitId: null,
        roomLevel: null,
        roomCode: null,
        roomName: null,
        roomAcreage: null,
        roomCost: null,
        roomStatus: null,
        roomIsShop: null,
        roomSCommercialHouse: null,
        roomHouseType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间 ";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      getRoom(roomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房间 ";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roomId != null) {
            updateRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomIds = row.roomId || this.ids;
      this.$modal.confirm('是否确认删除房间 编号为"' + roomIds + '"的数据项？').then(function() {
        return delRoom(roomIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/room/export', {
        ...this.queryParams
      }, `room_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
